package com.quasar.hpatient.view.personal.collection;

import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.CarouselData;
import com.quasar.hpatient.model.medicalmodel.CollectionListData;
import com.quasar.hpatient.model.medicalmodel.ConsultationBean;
import com.quasar.hpatient.network.ApiService;
import com.quasar.hpatient.onInterface.OnConsultationListener;
import com.quasar.hpatient.presenter.RecommendListPresenter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

/**
 * 咨询详情
 */
@EActivity(R.layout.activity_counsult_web_view)
public class CounsultWebViewActivity extends BaseActivity implements OnConsultationListener {

    @ViewById(R.id.webView1)
    WebView mwv;
    @ViewById(R.id.progressBar1)
    ProgressBar mjdpb;
    //传递的咨询ID
    @Extra
    String recordId;
    @ViewById(R.id.tv_title)
    TextView tv_title;
    //收藏图表
    @ViewById(R.id.iv_right_item)
    ImageView iv_right_item;


    private RecommendListPresenter recommendListPresenter;


    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        tv_title.setText("资讯详情");
        initWebView();
        initOnClik();
    }


    @Click(R.id.iv_right_item)
    public void iv_right_item() {

        if (recordId != null) {
            recommendListPresenter.UpdateMedicalInformationCollectData(recordId);
        }
    }

    @Click(R.id.ll_left)
    public void ll_left() {
        onBackPressed();
    }

    @Override
    protected void initData() {
        recommendListPresenter = new RecommendListPresenter(this);
        recommendListPresenter.GetCollectionInfoExistData(recordId);
        mwv.loadUrl(ApiService.ConsultationDetails + recordId);
    }

    private void initOnClik() {
        // WebView........
        mwv.setWebViewClient(new WebViewClient() {
            @Override
            //网页加载完成
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                mjdpb.setVisibility(View.GONE);
                super.onPageFinished(view, url);
            }

            // 网页加载失败，重新加载
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                mwv.loadUrl(ApiService.ConsultationDetails + recordId);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    private void initWebView() {
        // TODO Auto-generated method stub
        WebSettings webSettings = mwv.getSettings();
        webSettings.setJavaScriptEnabled(true); //支持 js
        //webSettings.setPluginsEnabled(true); //支持插件
        webSettings.setUseWideViewPort(false); //将图片调整到适合 webview 的大小
        webSettings.setSupportZoom(true); //支持缩放
        webSettings.supportMultipleWindows(); //多窗口
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭 webview 中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setNeedInitialFocus(true); //当 webview 调用 requestFocus 时为 webview 设置节点
        webSettings.setBuiltInZoomControls(true); //设置支持缩放
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过 JS 打开新窗口
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
    }

    @Override
    public void onConsultationListSuccess(AnotherResult<ConsultationBean> data) {

    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onConsultationRefreshUI() {

    }

    @Override
    public void onCarouselSuccess(AnotherResult<CarouselData> dataAnotherResult) {

    }

    @Override
    public void onMyConsultationListSuccess(AnotherResult<CollectionListData> dataAnotherResult) {

    }

    @Override
    public void onNottoCollectSuccess(DataStateResult dataStateResult) {

        if (dataStateResult.getList().equals("0")) {
            iv_right_item.setImageResource(R.mipmap.s78454);
        } else {
            iv_right_item.setImageResource(R.mipmap.ssss5245545);
        }
    }

    @Override
    public void onButtonCollectionSuccess(DataStateResult dataStateResult) {
        ViseLog.d("操作成功");
        if (dataStateResult.getMsgCode().equals("0x00000000")) {
            msg("操作成功");
            recommendListPresenter.GetCollectionInfoExistData(recordId);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //清空所有Cookie
        CookieSyncManager.createInstance(MainAppliaction.getInstance());  //Create a singleton CookieSyncManager within a context
        CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
        cookieManager.removeAllCookie();// Removes all cookies.
        CookieSyncManager.getInstance().sync(); // forces sync manager to sync now
        mwv.setWebChromeClient(null);
        mwv.setWebViewClient(null);
        mwv.getSettings().setJavaScriptEnabled(false);
        mwv.clearCache(true);
    }
}
